<?php


namespace App\Helpers;


use App\Models\ActionLog;
use App\Models\Divide\PdPdLog;
use App\Models\Mch\RegisterCode;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

trait AgentCommissionTrait
{
    protected function UserCommission($money,$order,$user,$child){
        if($user){ // 获取第一级 分佣
            $config = $this->selectLevelById($user->level, (int)$order->account_mode);
            info('第'.$child.'分佣:'.json_encode($user));
            if(!$config){
                return $money;
            }
            if($child==1&&$config&&$config->is_sale){ // 开启会员价则退出本次分佣
                return $money;
            }
            if($config&&$config->is_borker==0){
                return $money;
            }
            info('获取配置'.json_encode($config));
            if($config){
                $finialMoney = $this->calculateMoney($money,$config->first_ratio3);
                $lastMoney = $this->safeSubtract($money,$finialMoney);
                $log = [
                    'uid'=>$user->uid,
                    'gid'=>$user->gid,
                    'pid'=>$user->pid,
                    'sid'=>$order->account_mode,
                    'name'=>$user->name,
                    'money'=>$finialMoney,
                    'role'=>$user->role,
                    'order_id'=>$order->order_id,
                    'order_sid'=>$order->id,
                    'mid'=>$order->account_mode,
                    'avatar'=>$user->avatar,
                    'level'=>$user->level,
                    'surplus_money'=>0.00,
                    'all_money'=>$money,
                ];
                PdPdLog::query()->create($log);
                if($user){
                    $user->waitting_money = $this->addAmounts($user->waitting_money,$finialMoney);
                    $user->save();
                }
                return $lastMoney;
            }else{
                return 0;
            }

        }
    }
}
